package com.itheima.client;

import com.itheima.domain.Student;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
@SuppressWarnings("all")
public class DemoClient {
    public static void main(String[] args) throws Exception {
        //创建一个客户端对象;
        Socket s = new Socket("127.0.0.1",9898);
        //输出语句
        System.out.println("请选择你要完成的操作：");
        System.out.println("1.增加学生  2.删除学生 3.修改学生  4.查看所有学生 5.退出");
        //键盘录入
        Scanner sc = new Scanner(System.in);
        int choose = sc.nextInt();
        //判断;
        switch (choose){
            case 1:
                //增加
                break;
            case 2:
                //删除
                break;
            case 3:
                //修改
                break;
            case 4:
                //查看
                break;
            case 5:
                //退出
                s.close();
                return;

        }
    }
    //增加方法
    public static void add(Socket s ) throws IOException {
        //让用户输入姓名年龄和居住地
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生姓名：");
        String name = sc.nextLine();
        System.out.println("请输入学生年龄：");
        String age = sc.nextLine();
        System.out.println("请输入学生居住地：");
        String address = sc.nextLine();
        //拼接给服务器发的数据
        String str = "[1] "+name+" "+age+" "+address;
        //给服务发送数据
        OutputStream os = s.getOutputStream();
        os.write(str.getBytes());
        //接受服务器会送的数据
        byte[] arr = new byte[1024];
        InputStream is = s.getInputStream();
        int len = is.read(arr);
        //打印
        System.out.println(new String(arr,0,len));

    }
    //查看方法;
    public static void find(Socket s) throws IOException, ClassNotFoundException {
        //给服务发送信息
        OutputStream os = s.getOutputStream();
        os.write("[4] ".getBytes());
        //接受服务器发回来的信息
        ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
        ArrayList<Student> o = (ArrayList<Student>) ois.readObject();
        //判断集合有没有学生
        if(o.size()==0){
            System.out.println("系统中没有学生");
        }else{
            for (Student student : o) {
                System.out.println(student);
            }
        }

    }
    //删除方法;
    public static void remove(Socket s) throws IOException {
        //让用户输入要删除的学员学号
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学员学号：");
        String id = sc.nextLine();
        //把数据发送给服务器
        OutputStream os = s.getOutputStream();
        String str = "[2] "+id;
        os.write(str.getBytes());
        //接受服务器会送结果
        InputStream is = s.getInputStream();
        byte[] arr = new byte[1024];
        int len = is.read(arr);
        //打印
        System.out.println(new String(arr,0,len));

    }
    //修改方法;
    public static void set(Socket s) throws IOException {
        //让用户输入要修改的信息
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生学号：");
        String id = sc.nextLine();
        //先判断学号是否存在
        OutputStream os = s.getOutputStream();
        os.write(("[4] "+id).getBytes());
        //读取服务器发回来的结果
        InputStream is = s.getInputStream();
        byte[] arr = new byte[1024];
        int len = is.read(arr);
        String result = new String(arr,0,len);
        //把字符串转成boolean如果是true则继续输入
        if(Boolean.parseBoolean(result)){
            System.out.println("请输入新的学生姓名(如果保留原则请输入0)");
            String name = sc.nextLine();
            System.out.println("请输入新的学生年龄(如果保留原则请输入0)");
            String age = sc.nextLine();
            System.out.println("请输入新的学生居住地(如果保留原则请输入0)");
            String address = sc.nextLine();
            String str = "[3] "+id+" "+name+" "+age+" "+address;
            //把信息发送给服务器
            os.write(str.getBytes());
            //接受从服务端发回来的结果
            len = is.read(arr);
            System.out.println(new String(arr,0,len));
        }else{
            System.out.println("学号不存在");

        }
    }
}
